package com.ls.x.activity;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ls.x.R;
import com.ls.x.activity.beans.UserBean;
import com.ls.x.service.IUserService;
import com.ls.x.service.UserService;
import com.ls.x.tool.DatabaseHelper;
import com.ls.x.tool.EncryptTool;

public class RegisterActivity extends Activity {

	private DatabaseHelper dbHelper = null;
	// private SQLiteDatabase db = null;
	// private Cursor cursor = null;
	private EditText edt_username;
	private EditText edt_password;
	private EditText edt_checkpassword;
	private EditText edt_phonenumber;
	private EditText edt_email;
	private String username;
	private String password;
	private String checkpassword;
	private String phonenumber;
	private String email;
	private long id;
	private IUserService userService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.page_register);
		dbHelper = DatabaseHelper.getDBhelper(RegisterActivity.this);
		userService = new UserService(dbHelper);
		// db = dbHelper.getReadableDatabase();

		edt_username = (EditText) findViewById(R.id.edt_register_page_username);
		edt_password = (EditText) findViewById(R.id.edt_register_page_password);
		edt_checkpassword = (EditText) findViewById(R.id.edt_register_page_checkpassword);
		edt_phonenumber = (EditText) findViewById(R.id.edt_register_page_phone_nunber);
		edt_email = (EditText) findViewById(R.id.edt_register_page_email);
		String post_username = "";
		Intent intent = getIntent();
		Log.d("lijing", "getExtras:" + intent.hasExtra("username"));
		if (intent.hasExtra("username")) {
			post_username = intent.getExtras().getString("username");
		}
		edt_username.setText(post_username);

		Button btnPost = (Button) findViewById(R.id.btn_register_page_post);
		btnPost.setOnClickListener(new PostButtonOnClickListener());
		Button btnCancel = (Button) findViewById(R.id.btn_register_page_cancle);
		btnCancel.setOnClickListener(new CancelButtonOnClickListener());

	}

	protected void onDestroy() {
		super.onDestroy();
		Log.d("lijing", "RegisterActivity进入onDestroy()");
		super.onStop();
		// if (db != null) {
		// db.close();
		// }
		if (dbHelper != null) {
			dbHelper.close();
		}
		super.finish();
	}

	class PostButtonOnClickListener implements OnClickListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		@Override
		public void onClick(View v) {
			/*
			 * EditText edt_username; EditText edt_password; EditText
			 * edt_checkpassword; EditText edt_phonenumber; EditText edt_email;
			 */
			username = edt_username.getText().toString();
			if (!userService.isUserExist(username)) {
				// 如果用户不存在
				password = EncryptTool.getMD5Str(edt_password.getText()
						.toString());
				checkpassword = EncryptTool.getMD5Str(edt_checkpassword
						.getText().toString());
				if (password.equals("") || username.equals("")) {
					Toast.makeText(RegisterActivity.this, "用户名或密码不能为空！", 500)
							.show();
				} else if (password.equals(checkpassword)) {
					// 如果两次输入密码一致
					phonenumber = edt_phonenumber.getText().toString();
					email = edt_email.getText().toString();
					// id = (new Date()).getTime();

					UserBean userBean = new UserBean();
					userBean.setUsername(username);
					userBean.setPassword(password);
					userBean.setEmail(email);
					userBean.setPhoneNumber(phonenumber);

					userService.saveUserBean(userBean);

					// db.execSQL("insert into user(id,username,password,email,phone_number) values("
					// + id
					// + ",'"
					// + username
					// + "','"
					// + password
					// + "','"
					// + email + "','" + phonenumber + "')");
					if (userService.isUserExist(username)) {
						Toast.makeText(RegisterActivity.this,
								"用户 " + username + " 创建成功！", 500).show();
						listAllUser();
					}
					RegisterActivity.this.onDestroy();
				} else {
					Toast.makeText(RegisterActivity.this, "两次密码输入不一致，请重新输入！",
							500).show();
					edt_password.setText("");
					edt_checkpassword.setText("");
				}
			} else {
				Toast.makeText(RegisterActivity.this, "用户名已经存在，请修改！", 500)
						.show();
			}
		}
	}

	class CancelButtonOnClickListener implements OnClickListener {

		/*
		 * (non-Javadoc)
		 * 
		 * @see android.view.View.OnClickListener#onClick(android.view.View)
		 */
		@Override
		public void onClick(View v) {
			RegisterActivity.this.finish();
		}
	}

	public void listAllUser() {
		List<UserBean> userBeanList = userService.getAllUserBeanList();
		for (UserBean userBean : userBeanList) {
			Log.d("lijing", "username:" + userBean.getUsername());
			Log.d("lijing", "password:" + userBean.getPassword());
		}
	}
}
